package com.cym.controller.adminPage;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.cym.consts.NginxConst;
import com.cym.ext.TreeNode;
import com.cym.service.SettingService;
import com.cym.utils.BaseController;
import com.cym.utils.JsonResult;
import com.cym.utils.SystemTool;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@Mapping("/adminPage/root")
public class RootController extends BaseController {

    @Inject
    SettingService settingService;

    @Mapping("getWwwList")
    public List<TreeNode> getWwwList(String id) {
        boolean first = false;
        String htmlDir = "";
        if (StrUtil.isEmpty(id)) {
            id = "/";
            // id 为空则表示打开初始目录
            String basePath = settingService.get(NginxConst.NGINX_HTML_DIR);
            if (StrUtil.isNotEmpty(basePath)) {
                if (basePath.endsWith("/")) {
                    basePath = basePath.substring(0, basePath.length() - 1);
                }
                htmlDir = basePath.substring(basePath.lastIndexOf("/") + 1);
                id = basePath.substring(0, basePath.lastIndexOf("/"));
                first = true;
            }
        }

        List<TreeNode> list = new ArrayList<>();

        File[] fileList;
        if (SystemTool.isWindows() && "/".equals(id)) {
            fileList = File.listRoots();
        } else {
            fileList = new File(id).listFiles();
        }

        assert fileList != null;

        if (first) {
            // 过滤目录，只保留html根目录
            String finalHtmlDir = htmlDir;
            fileList = Arrays.stream(fileList).filter(f -> f.isDirectory() && f.getName().equals(finalHtmlDir)).toArray(File[]::new);
        }

        for (File temp : fileList) {

            TreeNode treeNode = new TreeNode();
            treeNode.setId(temp.getPath());
            if (FileUtil.isDirectory(treeNode.getId())) {
                // 文件夹要加个/
                treeNode.setId(treeNode.getId() + "/");
            }
            if (StrUtil.isNotEmpty(temp.getName())) {
                treeNode.setName(temp.getName());
            } else {
                treeNode.setName(temp.getPath());
            }

            if (temp.isDirectory()) {
                treeNode.setIsParent("true");
            } else {
                treeNode.setIsParent("false");
            }

            list.add(treeNode);

        }

        // 按文件夹进行排序
        list.sort((o1, o2) -> {

            if ("true".equals(o1.getIsParent()) && "false".equals(o2.getIsParent())) {
                return -1;
            }
            if ("false".equals(o1.getIsParent()) && "true".equals(o2.getIsParent())) {
                return 1;
            }

            return o1.getName().compareToIgnoreCase(o2.getName());
        });

        return list;
    }

    @Mapping("getList")
    public List<TreeNode> getList(String id) {
        if (StrUtil.isEmpty(id)) {
            id = "/";
        }

        List<TreeNode> list = new ArrayList<>();

        File[] fileList;
        if (SystemTool.isWindows() && "/".equals(id)) {
            fileList = File.listRoots();
        } else {
            fileList = new File(id).listFiles();
        }

        assert fileList != null;

        for (File temp : fileList) {

            TreeNode treeNode = new TreeNode();
            treeNode.setId(temp.getPath());
            if (FileUtil.isDirectory(treeNode.getId())) {
                // 文件夹要加个/
                treeNode.setId(treeNode.getId() + "/");
            }
            if (StrUtil.isNotEmpty(temp.getName())) {
                treeNode.setName(temp.getName());
            } else {
                treeNode.setName(temp.getPath());
            }

            if (temp.isDirectory()) {
                treeNode.setIsParent("true");
            } else {
                treeNode.setIsParent("false");
            }

            list.add(treeNode);

        }

        // 按文件夹进行排序
        list.sort((o1, o2) -> {

            if ("true".equals(o1.getIsParent()) && "false".equals(o2.getIsParent())) {
                return -1;
            }
            if ("false".equals(o1.getIsParent()) && "true".equals(o2.getIsParent())) {
                return 1;
            }

            return o1.getName().compareToIgnoreCase(o2.getName());
        });

        return list;
    }


    @Mapping("mkdir")
    public JsonResult mkdir(String dir, String name) {

        FileUtil.mkdir(dir + name);

        return renderSuccess();
    }
}
